package com.bang.study.aggs;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
import org.elasticsearch.search.aggregations.bucket.histogram.Histogram;
import org.elasticsearch.search.aggregations.bucket.histogram.InternalDateHistogram;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.avg.InternalAvg;
import org.elasticsearch.search.aggregations.metrics.cardinality.InternalCardinality;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.util.List;

/**
 * @ClassName: MuchAggsSearch
 * @author: BANG
 * @Date: 2020/01/15 16:57
 * @Version: 1.0
 * @Modified By:
 * @Desc: TODO
 * GET /tvs/sales/_search
 * {
 *   "size" : 0,
 *   "aggs" : {
 *       "months" : {
 *         "date_histogram": {
 *           "field": "sold_date",
 *           "interval": "month",
 *           "format": "yyyy-MM-dd"
 *         },
 *         "aggs": {
 *           "distinct_colors" : {
 *               "cardinality" : {
 *                 "field" : "brand"
 *               }
 *           }
 *         }
 *       }
 *   }
 * }
 */
public class CardinalityAggsSearch {


    public static void main(String[] args) throws Exception {
        Settings settings = Settings.builder()
                .put("cluster.name", "elasticsearch")
                .put("client.transport.sniff", true)
                .build();

        TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)
        );

        SearchResponse searchResponse = client.prepareSearch("tvs").setTypes("sales")
                .addAggregation(AggregationBuilders.dateHistogram("months")
                        .field("sold_date")
                        .format("yyyy-MM-dd")
                        .dateHistogramInterval(DateHistogramInterval.MONTH)
                        .subAggregation(AggregationBuilders.cardinality("distinct_colors").field("brand"))
                        )
                .setSize(0)
                .get();

        InternalDateHistogram months = searchResponse.getAggregations().get("months");
        List<Histogram.Bucket> buckets = months.getBuckets();
        for (Histogram.Bucket bucket : buckets) {
            InternalCardinality distinct_colors = bucket.getAggregations().get("distinct_colors");
            System.err.println(bucket.getKeyAsString()+" "+bucket.getDocCount()+" "+distinct_colors.getValueAsString());
        }

        client.close();

    }
}
